package com.aracky.common.vo;

import com.aracky.common.enums.ResultCodeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * @author zengzhiping
 * @Description: 公共返回类
 * @date 2022/7/14 16:29
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class CommonResult<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    /**
     * 无限制，返回
     * 没有状态码的新建状态码。
     *
     * @param resultCode
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> result(ResultCodeEnum resultCode) {
        CommonResult<T> result = new CommonResult<>();
        result.code = resultCode.getCode();
        result.message = resultCode.getMsg();
        return result;
    }


    public static <T> CommonResult<T> error(Integer code, String message) {
        Assert.isTrue(!ResultCodeEnum.SUCCESS.getCode().equals(code), "code 必须是错误的！");
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.message = message;
        return result;
    }

    public static <T> CommonResult<T> error(String message) {
        CommonResult<T> result = new CommonResult<>();
        result.code = ResultCodeEnum.ERROR.getCode();
        result.message = message;
        return result;
    }

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = ResultCodeEnum.SUCCESS.getCode();
        result.data = data;
        result.message = ResultCodeEnum.SUCCESS.getMsg();
        return result;
    }

    public static <T> CommonResult<T> success(String message) {
        CommonResult<T> result = new CommonResult<>();
        result.code = ResultCodeEnum.SUCCESS.getCode();
        result.message = message;
        return result;
    }

    public static <T> CommonResult<T> success(T data, String message) {
        CommonResult<T> result = new CommonResult<>();
        result.code = ResultCodeEnum.SUCCESS.getCode();
        result.message = message;
        result.data = data;
        return result;
    }

}
